'Función principal que carga cada uno de los SLOTS, inicializa todo y permite conmutar bancos.
'Además contiene 3 funciones que se llaman desde el Motor por falta de espacio en el Slot donde se aloj Motor.bas

Main()
STOP

#INCLUDE once "VarComp.bas"
#INCLUDE once <memcopy.bas>
#INCLUDE once <putchars.bas>
#INCLUDE once <winscroll.bas>

'Carga Bancos de memoria
Sub CargarModulos()
    PaginarMemoria(MOD_MUS)
    LOAD "" CODE $c000
    PaginarMemoria(MOD_CRY)
    LOAD "" CODE $c000
    PaginarMemoria(MOD_MEN)
    LOAD "" CODE $c000
    PaginarMemoria(MOD_INT)
    LOAD "" CODE $c000
    PaginarMemoria(MOD_JUE)
    LOAD "" CODE $c000
    PaginarMemoria(BANC_REC)
    LOAD "" CODE DIRVARSBLK0
    LOAD "" CODE DIRVARSBLK0_PANT
    LOAD "" CODE DIRVARSBLK0_TILES
    LOAD "" CODE DIRVARSBLK0_SPR_P
    LOAD "" CODE DIRVARSBLK0_SPR_PG
    LOAD "" CODE DIRVARSBLK0_SPR_SS
    LOAD "" CODE DIRVARSBLK0_STARS
    LOAD "" CODE DIRVARSBLK0_SPR_E
    LOAD "" CODE DIRVARSBLK0_TXT
    LOAD "" CODE DIRVARSBLK0_OBJECTS
END Sub

'Ejecuta el módulo seleccionado
Sub EjecutarModulo(modulo As UByte)
    PaginarMemoria(modulo)
    memcopy($c000,$8000,$4000)
    PaginarMemoria(0)
    PRINT AT 0,0;USR $8000;
END Sub

'Inicamos variables Comunes (y perennes dado que se alojan en $6000)
Sub IniciaVarComunes()
    TipoControl=1
    musica=0
    intro=1
    Teclas(0)=$df02 'q'
    Teclas(1)=$df01 'a'
    Teclas(2)=$fb01 'o'
    Teclas(3)=$fd01 'p'
    Teclas(4)=$7f01 'espacio'
    experience=0
    ending=0
    jugador=0
    CCFail=0
    panelgame=0
    paneltrn=0
    BORDER 0: PAPER 0: INK 7
END Sub

Dim enemCols(9) As UBYTE => {_
40,_
43,_
40,_
43,_
1,_
20,_
45,_
51,_
1,_
12}


Dim colFly(3) As UBYTE=> {$41,$03,$05,$41}
Dim colDrill(3) As UBYTE => {$45,$04,$45,$44}

Dim Conex_Inic(3,11) As UByte => {_
{0,1,2,3,4,5,6,7,8,9,10,11}, _
{12,13,14,15,16,17,18,19,20,21,22,23}, _
{24,25,26,27,28,29,30,31,32,33,34,35}, _
{36,37,38,39,40,41,42,43,44,45,46,47} _
}

'Pokea el array de Conex en memoria
Sub pokea()
    memcopy(@Conex_Inic,DIRSCONEX,48)
END Sub

'Inicia el mapa de Enemigos
Sub iniciaEnemigos()
    Dim p,e As UBYTE
    For p=0 To PANTALLAS_MAX
        For e=0 To 1
            MapaE(p,e,1)=DatosE(MapaE(p,e,0),1)
            MapaE(p,e,2)=DatosE(MapaE(p,e,0),2)
        Next e
    Next p
END Sub

'Transición entre escenas
Sub Transition()
    Dim i As UBYTE
    For i=0 To 16
        WinScrollLeft(2,0,16,16)
        WinScrollRight(2,16,16,16)
    Next i
END Sub

'Muestra la pantalla indicada en la variable perenne 'pantalla'
'configura colores de enemigos y animaciones
Sub imprPant()
    Dim x, y As UByte
    Dim tx, ty As UByte
    Dim dir As UByte
    Dim tile As UByte
    Dim e,i As UBYTE
    Dim tipo,mult,offsetMask As BYTE
    Dim zona As ubyte
    
    ink 0: PRINT AT 0,0;
    
    offsetMask=(pantalla mod 3)
    For e=0 To 1
        tipo=MapaE(pantalla,e,3)
        tile=enemCols(tipo)
        If tipo=4
            memcopy(@colFly,@colMask(e,0),4)
        Else If tipo=8
            memcopy(@colDrill,@colMask(e,0),4)
        Else
            memcopy(@tiles_attr(tile,0),@colMask(e,0),4)
        End If
        For i=0 To 3
            colMask(e,i)=colMask(e,i)+offsetMask
        Next i
    Next e
    
    For i=0 To nanim
        anim(i,0)=0
        anim(i,1)=0
        anim(i,2)=0
    Next i
    nanim=0
    
    giro(0,0)=0: giro(0,1)=WAITTURNING
    giro(1,0)=0: giro(1,1)=WAITTURNING
    
    zona=2
    If (pantalla>6 And pantalla<12) Or (pantalla>18 And pantalla<24)
        zona=0
    Else If (pantalla>30 And pantalla<36) Or (pantalla>42 And pantalla<48)
        zona=1
    End If
    
    dir=0
    ty=2
    For y=0 To 7
        tx=0
        For x=0 To 15
            tile=Pantallas(pantalla,dir)
            If tile=77 Or tile=78
                Pantallas(pantalla,dir)=0
            Else If tile<64
                PutChars(tx,ty,2,2,@tiles(tile,0))
                If ((tile>22 And tile<26) Or tile=27)
                    If zona<2
                        If tile=23 Or tile=25
                            memcopy(@colTmp(zona,0,0),@tiles_attr(tile,0),4)
                        Else If tile=24
                            memcopy(@colTmp(zona,1,0),@tiles_attr(tile,0),4)
                        Else If tile=27
                            memcopy(@colTmp(zona,2,0),@tiles_attr(tile,0),4)
                        End If
                    Else
                        memcopy(@colTmp(2,0,0),@tiles_attr(23,0),4)
                        memcopy(@colTmp(2,1,0),@tiles_attr(24,0),4)
                        memcopy(@colTmp(2,2,0),@tiles_attr(27,0),4)
                    End If
                End If
                PaintData(tx,ty,2,2,@tiles_attr(tile,0))
                If tile=4 Or tile=6 Or tile=7 Or tile=9 Or tile=10 Or tile=13  Or tile=14 Or _
                    tile=17 Or tile=18 Or tile=26 Or tile=29 Or tile=30 Or (tile>35 And tile<48)
                    anim(nanim,0)=tx
                    anim(nanim,1)=ty
                    anim(nanim,2)=tile
                    nanim=nanim+1
                End If
            Else If tile>63
                If tile<77
                    anim(nanim,0)=tx
                    anim(nanim,1)=ty
                    anim(nanim,2)=tile
                    nanim=nanim+1
                End If
                PutChars(tx,ty,2,2,@tiles(0,0))
                PaintData(tx,ty,2,2,@tiles_attr(0,0))
            End If
            dir=dir+1
            tx=tx+2
        Next x
        ty=ty+2
    Next y
    
    If pantalla=24
        PutChars(30,8,2,8,@nave_izq(0))
    Else If pantalla=25
        PutChars(0,8,2,8,@nave_der(0))
    Else If pantalla=47
        PutChars(26,8,2,8,@nave_izq(0))
        PutChars(28,8,2,8,@nave_der(0))
        PaintData(26,12,1,2,@tiles_attr(12,0))
        PaintData(29,12,1,2,@tiles_attr(12,0))
        PaintData(26,14,1,2,@tiles_attr(12,0))
        PaintData(29,14,1,2,@tiles_attr(12,0))
    End If
END Sub

'Inicia los colores de enemigo según pantalla
Sub iniciaCol()
    colTmp(0,0,0)=$47:colTmp(0,0,1)=$47:colTmp(0,0,2)=$2:colTmp(0,0,3)=$2
    colTmp(0,1,0)=2:colTmp(0,1,1)=66:colTmp(0,1,2)=2:colTmp(0,1,3)=66
    colTmp(0,2,0)=47:colTmp(0,2,1)=7:colTmp(0,2,2)=47:colTmp(0,2,3)=7
    colTmp(1,0,0)=$47:colTmp(1,0,1)=$47:colTmp(1,0,2)=$5:colTmp(1,0,3)=$5
    colTmp(1,1,0)=7:colTmp(1,1,1)=87:colTmp(1,1,2)=71:colTmp(1,1,3)=7
    colTmp(1,2,0)=79:colTmp(1,2,1)=1:colTmp(1,2,2)=79:colTmp(1,2,3)=1
    memcopy(@tiles_attr(23,0),@colTmp(2,0,0),4)
    memcopy(@tiles_attr(24,0),@colTmp(2,1,0),4)
    memcopy(@tiles_attr(27,0),@colTmp(2,2,0),4)
END Sub

'Función principal
Sub Main()
    model=peek 2899
    BORDER 0: PAPER 0: INK 2
    PRINT AT 3,0;"ZX Model: ";model
    PRINT "Loading Modules..."
    pokea()
    CargarModulos()
    PRINT "Initializing vars..."
    iniciaCol()
    IniciaVarComunes()
    PRINT "Finishing..."
    pause 100
    pantalla=100
    If pantalla<50
        imprPant()
        iniciaEnemigos()
        Transition()
    End If
    MODRun = MOD_MUS
    Do
        EjecutarModulo(MODRun)
    Loop
END Sub











